package com.imooc.bilibili.api;

import com.imooc.bilibili.domain.core.JsonResponse;
import com.imooc.bilibili.file.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author gcq
 * @Create 2022-06-04
 */
@RestController
public class FileApi {

    @Autowired
    private IFileService fileService;

    @GetMapping("md5files")
    public JsonResponse<String> getFileMd5(MultipartFile file) throws Exception {
        String fileMd5 = fileService.getFileMd5(file);
        return new JsonResponse<>(fileMd5);
    }

    @PutMapping("/file-slices")
     public JsonResponse<String> uploadFileBySlices(MultipartFile file,
                                                   String fileMd5,
                                                   Integer sliceNo,
                                                   Integer totalSliceNo) throws IOException {
        String filePath = fileService.uploadFileBySlices(file, fileMd5, sliceNo, totalSliceNo);
        return new JsonResponse<>(filePath);
    }
}